package Hashtable;

import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.IntStream;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class twoSum {
    public int[] twoSum(int[] nums, int target) {
//        int a = 0, b = 0;
//        for (int fast = 0; fast < nums.length; fast++) {
//            for (int slow = fast+1; slow < nums.length; slow++) {
//                if (nums[fast] + nums[slow] == target){
//                    a = fast;
//                    b = slow;
//                    break;
//                }
//            }
//        }
//        return IntStream.of(a, b).toArray();
        HashMap<Integer, Integer> map = new HashMap<>();
        int a = 0, b = 0;
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp)){
                a = i;
                b = map.get(temp);
                break;
            }
            map.put(nums[i], i);
        }
        return IntStream.of(a, b).toArray();
    }
}
